package student.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import manager.actionform.StudentForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import dao.StudentMagDao;
import dao.impl.StudentMagImpl;
import domain.Student;

/**
 * 学生修改个人信息的Action
 * 
 * May 26, 2009
 *
 * @author Fabian
 *
 */
public class ManagerSelfInfoAction extends DispatchAction {
	/**
	 *查看学生个人信息
	 */
	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		//获取Session中的Manager的id
		HttpSession session = request.getSession();
		Student stu = null;
		stu = (Student)session.getAttribute("user");
		int s_id = stu.getId();
		
		StudentMagDao sm = new StudentMagImpl();
		stu = sm.getStudent(s_id);
		
		String jsonStr = "";
		if (stu != null) {
			//组装返回的json数据
			jsonStr = jsonStr + "{root: [{id: " + stu.getId() + ", number: " + stu.getNumber() +
			", name: '" + stu.getName() + "', sex: '" + stu.getSex() + "', password: '" +
			stu.getPassword() + "', banji: '" + stu.getClass_id() +  "', grade: '" + stu.getGrade() + " 级', grade_id: " + 
			stu.getGrade_id() + ", major: '" + stu.getMajor() + "', major_id: " + stu.getMajor_id() + "}]}";
		}
		request.setAttribute("result", jsonStr);
		
		return mapping.findForward("show_result");
	}
	/**
	 * 获取学生个人信息，返回，用于修改个人信息
	 */
	public ActionForward get(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获取Session中的Manager的id
		HttpSession session = request.getSession();
		Student stu = null;
		stu = (Student)session.getAttribute("user");
		
		int s_id = stu.getId();
		
		StudentMagDao sm = new StudentMagImpl();
		stu = sm.getStudent(s_id);		
		
		//组装返回的JSON数据		
		String jsonStr = "";
		
		if (stu != null) {
			jsonStr = jsonStr + "{id: " + stu.getId() + ", number: " + stu.getNumber() +
			", name: '" + stu.getName() + "', sex: '" + stu.getSex() + "', password: '" +
			stu.getPassword() + "', banji: '" + stu.getClass_id() +  "', grade: '" + stu.getGrade() + " 级', grade_id: " + 
			stu.getGrade_id() + ", major: '" + stu.getMajor() + "', major_id: " + stu.getMajor_id() + "}";
		}
		
		request.setAttribute("result", jsonStr);		
		
		return mapping.findForward("get_result");
	}
	
	/**
	 * 修改学生个人信息 
	 */
	public ActionForward modify(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		HttpSession session = request.getSession();
		Student stu = null;
		stu = (Student)session.getAttribute("user");
		
		StudentForm sf = (StudentForm)form;
		//stu.setName(sf.getName());
		//stu.setSex(sf.getSex());
		//stu.setNumber(sf.getNumber());
		stu.setPassword(sf.getPassword());
		
		boolean result = false;
		
		if (stu != null) {
			StudentMagDao sm = new StudentMagImpl();
			result = sm.modifyStudent(stu);
		}		
		
		//组装返回的JSON数据
		String returnStr = "";
		if (result) {
			
			returnStr = "{success: true, msg: '恭喜，个人信息修改成功！请重新登陆！'}";
			request.setAttribute("result", returnStr);
			
		} else {
			
			returnStr = "{success: false, msg: '抱歉，个人信息修改失败！请重新尝试！'}";
			request.setAttribute("result", returnStr);
			
		}	
		return mapping.findForward("modify_result");
	}
}
